package com.web.boot.web.cms;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.http.message.ViewBuilder;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.cms.doc.facade.ChannelFacade;
import com.web.cms.doc.facade.WebFacade;
import com.web.cms.doc.message.ChannelRequest;
import com.web.cms.doc.message.ChannelResponse;
import com.web.cms.doc.message.WebResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

@Controller("cmsChannelController")
@Http(url = "/cms")
public class ChannelController
{
	@Autowired
	private ChannelFacade channelFacade;
	@Autowired
	private WebFacade webFacade;
	
	private DictParser dictParser = DictParser.getInstance();
	
	public ChannelController()
	{
	}
	
	@Http(url = "/channel_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		ChannelRequest channelRequest = request.getQueryObject(ChannelRequest.class);
		List<WebResponse> webList = webFacade.listAll();
		if (channelRequest.getWebId() == null && !webList.isEmpty())
		{
			channelRequest.setWebId(webList.get(0).getId());
		}
		return View.createBuilder().setView("/cms/channel_list")
				.put("model", channelRequest)
				.put("treeList", channelFacade.listTree(channelRequest.getWebId(), null))
				.put("map", dictParser.getStringMap())
				.put("webList", webFacade.listAll())
				.build();
	}
	
	@Http(url = "/channel_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		ChannelRequest channelRequest = request.getQueryObject(ChannelRequest.class);
		ChannelResponse response = null;
		if (channelRequest.getId() != null)
		{
			response = channelFacade.get(channelRequest.getId());
			channelRequest.setWebId(response.getWebId());
			channelRequest.setParentId(response.getParentId());
		}
		ViewBuilder builder = View.createBuilder();
		if (channelRequest.getParentId() != null)
		{
			builder.put("parentModel", channelFacade.get(channelRequest.getParentId()));
		}
		if (channelRequest.getWebId() != null)
		{
			builder.put("web", webFacade.get(channelRequest.getWebId()));
		}
		return builder.setView("/cms/channel_get")
				.put("model", response == null ? new ChannelResponse() : response)
				.put("map", dictParser.getStringMap())
				.put("webList", webFacade.listAll())
				.build();
	}
	
	@Http(url = "/channel_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		ChannelRequest channelRequest = request.getPostObject(ChannelRequest.class);
		if (channelRequest.getId() == null)
		{
			ValidationUtil.valid(channelRequest, SaveModel.class);
			channelFacade.add(channelRequest);
		}
		else
		{
			ValidationUtil.valid(channelRequest, UpdateModel.class);
			channelFacade.update(channelRequest);
		}
	}

	@Http(url = "/channel_refresh", method = HttpMethod.POST)
	public void refresh(HttpRequest request)
	{
		channelFacade.refresh();
	}
	
}
